/*
 * TCCC 2003 Semifinals3 Prob1
 */

import java.util.*;
import java.util.regex.*;
import java.text.*;
import java.math.*;


public class ZigZag
{
    public int longestZigZag(int[] sequence)
    {
	int n = sequence.length;
	int[] len1 = new int[n];
	int[] len2 = new int[n];
	Arrays.fill (len1, 1);
	Arrays.fill (len2, 1);
	int max = 1;
	for (int i = 1; i < n; ++i) {
	    for (int j = i - 1; j >= 0; --j) {
		if (sequence[j] < sequence[i]) {
		    len1[i] = Math.max (len1[i], 1 + len2[j]);
		    if (len1[i] > max)
			max = len1[i];
		} else if (sequence[j] > sequence[i]) {
		    len2[i] = Math.max (len2[i], 1 + len1[j]);
		    if (len2[i] > max)
			max = len2[i];
		}
	    }
	}
	return max;
    }
    

}
//Powered by KawigiEdit 2.1.4 (beta) modified by pivanof!